package com.jingxing.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jingxing.config.ApiResult;
import com.jingxing.config.UserLoginToken;
import com.jingxing.pojo.WorksInfo;
import com.jingxing.service.WorksInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/worksInfo")
@Api(value = "API", tags = {"作品相关"})
@UserLoginToken
public class WorksInfoController {
    @Autowired
    private WorksInfoService worksInfoService;

    @PostMapping("/saveWorksInfo")
    @ApiOperation("添加作品")
    public ApiResult savePlayerUserInfo(String playerId, Integer competitionLevel, String workName, String workUrl, String shareLinks) {
        if (StringUtils.isBlank(playerId)) {
            return ApiResult.parameterError("选手ID不能为空");
        }
        if (competitionLevel == null) {
            return ApiResult.parameterError("比赛等级不能为空");
        }
        if (StringUtils.isBlank(workName)) {
            return ApiResult.parameterError("作品名不能为空");
        }
        if (StringUtils.isBlank(workUrl)) {
            return ApiResult.parameterError("作品地址不能为空");
        }
        WorksInfo worksInfo = new WorksInfo();
        worksInfo.setPlayerId(playerId);
        worksInfo.setCompetitionLevel(competitionLevel);
        worksInfo.setWorkName(workName);
        worksInfo.setWorkUrl(workUrl);
        if (StringUtils.isNotBlank(shareLinks)) {
            worksInfo.setShareLinks(shareLinks);
        }
        boolean save = worksInfoService.save(worksInfo);
        return ApiResult.result(save);
    }

    @PostMapping("/getWorksInfosByPlayerId")
    @ApiOperation("获取通过选手id获取选手作品")
    public ApiResult getWorksInfosByPlayerId(String playerId, Integer competitionLevel) {
        QueryWrapper<WorksInfo> queryWrapper = new QueryWrapper();
        queryWrapper.eq("player_id", playerId);
        if (competitionLevel != null) {
            queryWrapper.eq("competition_level", competitionLevel);
        }
        List<WorksInfo> list = worksInfoService.list(queryWrapper);
        return ApiResult.ok(list);
    }

    @PostMapping("/updateWorksInfo")
    @ApiOperation("修改作品分享链接")
    public ApiResult updateWorksInfo(String id, String shareLinks) {
        if (StringUtils.isBlank(id)) {
            return ApiResult.parameterError("ID不能为空");
        }
        WorksInfo worksInfo = new WorksInfo();
        worksInfo.setId(id);
        worksInfo.setShareLinks(shareLinks);
        boolean update = worksInfoService.updateById(worksInfo);
        return ApiResult.result(update);
    }

    @PostMapping("/deleteWorksInfo")
    @ApiOperation("删除作品")
    public ApiResult deleteWorksInfo(String id) {
        if (StringUtils.isBlank(id)) {
            return ApiResult.parameterError("ID不能为空");
        }
        boolean remove = worksInfoService.removeById(id);
        return ApiResult.result(remove);
    }


}
